package org.hhttp.request;

import java.io.InputStream;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;
import org.hhttp.core.Constants;
import org.hhttp.core.Request;
import org.hhttp.objectPool.BoundedBlockingPool;

public class RequestPool {
	
	private static Logger logger = Logger.getLogger(RequestPool.class);
	
	private static BoundedBlockingPool<Request> pool;

	public static Request get(InputStream inputStream){
		Request request = null;
		try {
			if(pool == null){
				pool = new BoundedBlockingPool<Request>(Constants.object_poll_num, 
						new RequestValidator() , 
						new RequestFactory(), 
						Executors.newCachedThreadPool());
			}
			request = pool.get();
			request.setInputStream(inputStream);
			request.init();
		} catch (Exception e) {
			logger.error(e);
		}
		return request;
	}
	
	public static void release(Request t){
		if(pool != null){
			t.reset();
			pool.release(t);
		}
	}
}
